package com.augurit.common.system.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.augurit.common.base.extmodel.base.LinkElement;
import com.augurit.common.base.extmodel.base.ITreeNode;
import com.augurit.common.base.extmodel.inner.MenuLinkTreeNode;
import com.augurit.common.constant.OrgConstant;
import com.augurit.common.system.web.org.form.AcMenuForm;
import com.augurit.common.system.web.org.form.AcModuleForm;
import com.augurit.common.system.web.org.form.OmOrgForm;
import com.augurit.common.system.web.org.form.OmPositionForm;

public class TreeUtils {
	
	/**
	 * 将无树结构组织的对象列表转换为以树结构组织的对象列表
	 * @param list 无树结构组织的对象列表
	 * @return
	 */
	public static List<AcMenuForm> constructMenuTree(List<AcMenuForm> list){
		List<AcMenuForm> result = null;
		
		if(list != null && list.size() > 0){
			
			//为树节点添加子节点
			for(int i=0; i<list.size(); i++)
				addChildren(list.get(i), list);
			
			//取出最顶层树节点
			result = new ArrayList();
			int minMenuLevel = OrgUtils.getMinMenuLevel(list);
			for(int i=0; i<list.size(); i++){
				if(list.get(i).getMenuLevel().intValue() == minMenuLevel){
					result.add(list.get(i));
				}
			}
		}
		
		return result;
	}

	/**
	 * 为form对象添加子节点及其子子节点
	 * @param form 要添加子节点的form对象
	 * @param listIncludeChildren 包含form对象子节点和子子节点的对象列表
	 */
	public static void addChildren(AcMenuForm form, List<AcMenuForm> listIncludeChildren){
		if(form != null){
			
			if(form.getChildren() == null)
				form.setChildren(new ArrayList<AcMenuForm>());
			
			for(int i=0; i<listIncludeChildren.size(); i++){
				if(listIncludeChildren.get(i).getParentMenuId() != null 
						&& listIncludeChildren.get(i).getParentMenuId().longValue() == form.getMenuId().longValue()
						&& !OrgUtils.exist(listIncludeChildren.get(i), form.getChildren())){
					form.getChildren().add(listIncludeChildren.get(i));
				}
			}
			
			for(int i=0; i<form.getChildren().size(); i++){
				addChildren(form.getChildren().get(i), listIncludeChildren);
			}
		}
	}
	
	
	/**
	 * 将无树结构组织的对象列表转换为以树结构组织的对象列表
	 * @param list 无树结构组织的对象列表
	 * @return
	 */
	public static List<MenuLinkTreeNode> constructExtNodeTree(List<MenuLinkTreeNode> list){
		List<MenuLinkTreeNode> result = null;
		
		if(list != null && list.size() > 0){
			
			//为树节点添加子节点
			for(int i=0; i<list.size(); i++)
				addChildren(list.get(i), list);
			
			//取出最顶层树节点
			result = new ArrayList();
			int minMenuLevel = OrgUtils.getMinNodeLevel(list);
			for(int i=0; i<list.size(); i++){
				if(list.get(i).getLevel().intValue() == minMenuLevel){
					result.add(list.get(i));
				}
			}
		}
		
		return result;
	}

	
	
	/**
	 * 将无树结构组织的对象列表转换为以树结构组织的对象列表
	 * @param list 无树结构组织的对象列表
	 * @return
	 */
	public static List<OmOrgForm> constructOrgTree(List<OmOrgForm> list){
		List<OmOrgForm> result = null;
		
		if(list != null && list.size() > 0){
			
			//为树节点添加子节点
			for(int i=0; i<list.size(); i++)
				addChildren(list.get(i), list);
			
			//取出最顶层树节点
			result = new ArrayList();
			int minMenuLevel = OrgUtils.getMinOrgLevel(list);
			for(int i=0; i<list.size(); i++){
				if(list.get(i).getOrgLevel().intValue() == minMenuLevel){
					result.add(list.get(i));
				}
			}
		}
		return result;
	}
	
	
	/**
	 * 将无树结构组织的对象列表转换为以树结构组织的对象列表
	 * @param list 无树结构组织的对象列表
	 * @return
	 */
	public static List<OmPositionForm> constructPositionTree(List<OmPositionForm> list){
		List<OmPositionForm> result = null;
		
		if(list != null && list.size() > 0){
			
			//为树节点添加子节点
			for(int i=0; i<list.size(); i++)
				addChildren(list.get(i), list);
			
			//取出最顶层树节点
			result = new ArrayList();
			int minMenuLevel = OrgUtils.getMinPosLevel(list);
			for(int i=0; i<list.size(); i++){
				if(list.get(i).getPosLevel().intValue() == minMenuLevel){
					result.add(list.get(i));
				}
			}
		}
		return result;
	}
	
	
	
	/**
	 * 为form对象添加子节点及其子子节点
	 * @param form 要添加子节点的form对象
	 * @param listIncludeChildren 包含form对象子节点和子子节点的对象列表
	 */
	public static void addChildren(OmPositionForm form, List<OmPositionForm> listIncludeChildren){
		if(form != null){
			
			if(form.getChildren() == null)
				form.setChildren(new ArrayList<OmPositionForm>());
			
			for(int i=0; i<listIncludeChildren.size(); i++){
				if(listIncludeChildren.get(i).getParentPosId() != null 
						&& listIncludeChildren.get(i).getParentPosId().equals(form.getPosId())
						&& !OrgUtils.exist(listIncludeChildren.get(i), form.getChildren())){
					form.getChildren().add(listIncludeChildren.get(i));
				}
			}
			
			for(int i=0; i<form.getChildren().size(); i++){
				addChildren(form.getChildren().get(i), listIncludeChildren);
			}
		}
	}
	
	/**
	 * 为form对象添加子节点及其子子节点
	 * @param form 要添加子节点的form对象
	 * @param listIncludeChildren 包含form对象子节点和子子节点的对象列表
	 */
	public static void addChildren(OmOrgForm form, List<OmOrgForm> listIncludeChildren){
		if(form != null){
			
			if(form.getChildren() == null)
				form.setChildren(new ArrayList<OmOrgForm>());
			
			for(int i=0; i<listIncludeChildren.size(); i++){
				if(listIncludeChildren.get(i).getParentOrgId() != null 
						&& listIncludeChildren.get(i).getParentOrgId().equals(form.getOrgId())
						&& !OrgUtils.exist(listIncludeChildren.get(i), form.getChildren())){
					form.getChildren().add(listIncludeChildren.get(i));
				}
			}
			
			for(int i=0; i<form.getChildren().size(); i++){
				addChildren(form.getChildren().get(i), listIncludeChildren);
			}
		}
	}
	
	
	
	/**
	 * 为form对象添加子节点及其子子节点
	 * @param form 要添加子节点的form对象
	 * @param listIncludeChildren 包含form对象子节点和子子节点的对象列表
	 */
	public static void addChildren(MenuLinkTreeNode form, List<MenuLinkTreeNode> listIncludeChildren){
		if(form != null){
			
			if(form.getChildren() == null)
				form.setChildren(new ArrayList<MenuLinkTreeNode>());
			
			for(int i=0; i<listIncludeChildren.size(); i++){
				if(listIncludeChildren.get(i).getParentId() != null 
						&& listIncludeChildren.get(i).getParentId().equals(form.getId())
						&& !OrgUtils.exist(listIncludeChildren.get(i), form.getChildren())){
					form.getChildren().add(listIncludeChildren.get(i));
				}
			}
			
			for(int i=0; i<form.getChildren().size(); i++){
				addChildren(form.getChildren().get(i), listIncludeChildren);
			}
		}
	}
	
	
	/**
	 * 为form对象添加子节点及其子子节点
	 * @param form 要添加子节点的form对象
	 * @param listIncludeChildren 包含form对象子节点和子子节点的对象列表
	 */
	public static void addChildren(AcModuleForm form, List<AcModuleForm> listIncludeChildren){
		if(form != null){
			
			//遍历listIncludeChildren，把form的孩子结点找出来加入到children中
			for(int i=0; i<listIncludeChildren.size(); i++){
				if(listIncludeChildren.get(i).getParentModuleId() != null 
						&& listIncludeChildren.get(i).getParentModuleId().equals(form.getModuleId())){
					
					if(form.getChildren() == null)
						form.setChildren(new ArrayList<AcModuleForm>());
					
					form.getChildren().add(listIncludeChildren.get(i));
				}
			}
			
			//递归构造form的所有孩子结点
			if(form.getChildren() != null){
				for(int i=0; i<form.getChildren().size(); i++){
					if(form.getChildren().get(i).getIsLeaf().equals(OrgConstant.NOT_LEAF_NODE))
						addChildren(form.getChildren().get(i), listIncludeChildren);
				}
			}
		}
	}
	
	/**
	 * 将应用下的模块按照树状结构进行组织
	 * @param list：应用下的所有模块列表，包括直属模块和子模块
	 * @return：返回具有层次结构的直属模块的列表
	 */
	public static List<AcModuleForm> constructModuleTree(List<AcModuleForm> list){
		List<AcModuleForm> result = null;
		int minMenuLevel = OrgUtils.getMinModuleLevel(list);
		if(list != null && list.size() > 0){
			
			//为树节点添加子节点
			for(int i=0; i<list.size(); i++)
				if(list.get(i).getModuleLevel().intValue() == minMenuLevel)
					addChildren(list.get(i), list);
			
			//取出最顶层树节点
			result = new ArrayList();
			
			for(int i=0; i<list.size(); i++){
				if(list.get(i).getModuleLevel().intValue() == minMenuLevel){
					result.add(list.get(i));
				}
			}
		}
		return result;
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<AcMenuForm> list = new ArrayList();
		
		AcMenuForm form12 = new AcMenuForm();
		form12.setMenuId(new Long(12));
		form12.setParentMenuId(new Long(1));
		form12.setMenuLevel(2);
		form12.setMenuCode("test12");
		list.add(form12);
		
		AcMenuForm form132 = new AcMenuForm();
		form132.setMenuId(new Long(132));
		form132.setParentMenuId(new Long(13));
		form132.setMenuLevel(3);
		form132.setMenuCode("test132");
		list.add(form132);
		
		AcMenuForm form1321 = new AcMenuForm();
		form1321.setMenuId(new Long(1321));
		form1321.setParentMenuId(new Long(132));
		form1321.setMenuLevel(4);
		form1321.setMenuCode("test1321");
		list.add(form1321);
		
		AcMenuForm form13 = new AcMenuForm();
		form13.setMenuId(new Long(13));
		form13.setParentMenuId(new Long(1));
		form13.setMenuLevel(2);
		form13.setMenuCode("test13");
		list.add(form13);
		
		AcMenuForm form1 = new AcMenuForm();
		form1.setMenuId(new Long(1));
		form1.setParentMenuId(null);
		form1.setMenuLevel(1);
		form1.setMenuCode("test1");
		list.add(form1);
		
		AcMenuForm form131 = new AcMenuForm();
		form131.setMenuId(new Long(131));
		form131.setParentMenuId(new Long(13));
		form131.setMenuLevel(3);
		form131.setMenuCode("test131");
		list.add(form131);
		
		AcMenuForm form1311 = new AcMenuForm();
		form1311.setMenuId(new Long(1311));
		form1311.setParentMenuId(new Long(131));
		form1311.setMenuLevel(4);
		form1311.setMenuCode("test1311");
		list.add(form1311);
		
		AcMenuForm form1312 = new AcMenuForm();
		form1312.setMenuId(new Long(1312));
		form1312.setParentMenuId(new Long(131));
		form1312.setMenuLevel(4);
		form1312.setMenuCode("test1312");
		list.add(form1312);
		
		AcMenuForm form1313 = new AcMenuForm();
		form1313.setMenuId(new Long(1313));
		form1313.setParentMenuId(new Long(131));
		form1313.setMenuLevel(4);
		form1313.setMenuCode("test1313");
		list.add(form1313);
		
		AcMenuForm form2 = new AcMenuForm();
		form2.setMenuId(new Long(2));
		form2.setParentMenuId(null);
		form2.setMenuLevel(1);
		form2.setMenuCode("test2");
		list.add(form2);
		
		AcMenuForm form23 = new AcMenuForm();
		form23.setMenuId(new Long(23));
		form23.setParentMenuId(new Long(2));
		form23.setMenuLevel(2);
		form23.setMenuCode("test23");
		list.add(form23);
		
		
		long start = Calendar.getInstance().getTimeInMillis();
		List<AcMenuForm> result = constructMenuTree(list);
		System.out.println("" + (Calendar.getInstance().getTimeInMillis() - start));
		
		LinkElement node = new LinkElement();
		System.out.println(node instanceof ITreeNode);
	}
}
